AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')
local Ground = 1 + 0 + 2 + 8 + 32
local rel = "vehicles/tank_readyfire1.wav"

function ENT:Initialize()
	self.orginalposition = self.Entity:GetPos()
	self.Entity:SetModel(  "models/roller_spikes.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType(  MOVETYPE_VPHYSICS   )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	local pos = self.Entity:GetPos()
	for var=1, 20, 1 do 
		local zonetype = "Sector" .. var
		if	CheckPoint(pos.X,pos.Y,pos.Z,zonetype) then
			self.currentsector = var
		end
	end
	self.Physik = self.Entity:GetPhysicsObject()
	self.Entity:StartMotionController()
	self.exploded = false
	self.hp = 200
end


function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
	local ent = ents.Create( "mine" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:Think()
	self.Entity:NextThink( CurTime() + 2 )
	for index,ply in pairs(player.GetAll( )) do	
		local plyuni = ply:UniqueID( )
		if (servertable["ply"][plyuni]["currentsector"] == self.currentsector) then
			for k,core in pairs(servertable["ship"]["shipcores"]) do 
				if IsOwner(core,ply) then
					self.Entity:NextThink( CurTime() + 0.25 )
					local pos = self.Entity:GetPos()
					local corepos = core:GetPos()
					local move = Vector(0,0,0)
					move = move + self.Entity:GetForward( )*200
					local aimangle = (corepos - pos):Normalize()
					self.Angles = aimangle:Angle( )
					self.pos = pos + move
					self.Physik:Wake()
				end
			end
		end
	end	
	return true
end

function ENT:PhysicsSimulate() 
 	self.Physik:Wake()
	self.ShadowParams={} 
	self.ShadowParams.secondstoarrive	= 1
	self.ShadowParams.pos			= self.pos
	self.ShadowParams.angle			= self.Angles
	self.ShadowParams.maxangular		= 15
	self.ShadowParams.maxangulardamp	= 1000
	self.ShadowParams.maxspeed		= 200
	self.ShadowParams.maxspeeddamp		= 1000
	self.ShadowParams.dampfactor		= 0.7
	self.ShadowParams.teleportdistance	= 0
	self.ShadowParams.deltatime		= 10
	self.Physik:ComputeShadowControl(self.ShadowParams) 
 end  

function ENT:PhysicsCollide( bla,bla2)
	self:Explode()
end
 
function ENT:Explode()
	if not self.exploded then
		self.exploded = true
		timer.Create("Respawn" .. tostring(self.orginalposition),30,1,respawnnpc,"mine",self.orginalposition)
		local effectdata = EffectData()
		effectdata:SetStart( self.Entity:GetPos() )
		effectdata:SetOrigin( self.Entity:GetPos() )
		effectdata:SetScale( 300 )
		effectdata:SetMagnitude( 400 )
		effectdata:SetEntity(self.Entity)
		util.Effect( "Plasme_exp_small", effectdata )
		util.BlastDamage(self.Entity,self.Entity,self.Entity:GetPos(), 300, 1000)
		self:Fire("kill",0,0.3) 	
	end
end 

function ENT:OnTakeDamage(info)
	if not self.exploded then
		self.hp = self.hp  - info:GetDamage()
		if (self.hp < 1) then
			self:Explode()
		end
	end
end


